package com.ztf.reg;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class Test {

	public static void main(String[] args) {
		//简单认识正则表达式的概念
		/*
		p("abc".matches("..."));
		p("a8729a".replaceAll("\\d", "-"));
		Pattern p = Pattern.compile("[a-z]{3}");
		Matcher m = p.matcher("fgh");
		p(m.matches());
		p("fgha".matches("[a-z]{3}"));
		*/

		//初步认识. * + ?
		/*
		p("a".matches("."));
		p("aa".matches("aa"));
		p("aaaa".matches("a*"));
		p("aaaa".matches("a+"));
		p("".matches("a*"));
		p("aaaa".matches("a?"));
		p("".matches("a?"));
		p("a".matches("a?"));
		p("214523145234532".matches("\\d{3,100}"));
		p("192.168.0.aaa".matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}"));
		p("192".matches("[0-2][0-9][0-9]"));
		*/

		//范围
		/*
		p("a".matches("[abc]"));
		p("a".matches("[^abc]"));
		p("A".matches("[a-zA-Z]"));
		p("A".matches("[a-z]|[A-Z]"));
		p("A".matches("[a-z[A-Z]]"));
		p("R".matches("[A-Z&&[RFG]]"));
		*/

		//认识\s \w \d \
		/*
		p(" \n\r\t".matches("\\s{4}"));
		p(" ".matches("\\S"));
		p("a_8".matches("\\w{3}"));
		p("abc888&^%".matches("[a-z]{1,3}\\d+[&^#%]+"));
		p("\\".matches("\\\\"));
		*/

		//POSIX Style
		//p("a".matches("\\p{Lower}"));

		//boundary
		/*
		p("hello sir".matches("^h.*"));
		p("hello sir".matches(".*ir$"));
		p("hello sir".matches("^h[a-z]{1,3}o\\b.*"));
		p("hellosir".matches("^h[a-z]{1,3}o\\b.*"));
		//whilte lines
		p(" \n".matches("^[\\s&&[^\\n]]*\\n$"));

		p("aaa 8888c".matches(".*\\d{4}."));
		p("aaa 8888c".matches(".*\\b\\d{4}."));
		p("aaa8888c".matches(".*\\d{4}."));
		p("aaa8888c".matches(".*\\b\\d{4}."));
		*/

		//email
		//p("asdfasdfsafsf@dsdfsdf.com".matches("[\\w[.-]]+@[\\w[.-]]+\\.[\\w]+"));

		//matches find lookingAt
		/*
		Pattern p = Pattern.compile("\\d{3,5}");
		String s = "123-34345-234-";
		Matcher m = p.matcher(s);
		p(m.matches());
		m.reset();
		p("字符串长度="+s.length());
		p(m.find());
		p(m.start() + "-" + m.end());
		p(s.substring(3,4));
		p(m.find());
		p(m.start() + "-" + m.end());
		p(m.find());
		p(m.start() + "-" + m.end());
		p(m.find());
		//p(m.start() + "-" + m.end());
		m.reset();
		p(m.lookingAt());
		p(m.lookingAt());
		p(m.lookingAt());
		p(m.lookingAt());
		*/

		//replacement
       /*
		Pattern p = Pattern.compile("java", Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher("java Java JAVa JaVa IloveJAVA you hateJava afasdfasdf");
		StringBuffer buf = new StringBuffer();
		int i=0;
		while(m.find()) {
			p(m.group());
			i++;
			if(i%2 == 0) {
				m.appendReplacement(buf, "java1");
			} else {
				m.appendReplacement(buf, "JAVA");
			}
		}
		m.appendTail(buf);
		p(buf);
		*/

		//group
		/*Pattern p = Pattern.compile("(\\d{3,5})([a-z]{2})");
		String s = "123aa-34345bb-234cc-00";
		Matcher m = p.matcher(s);
		while(m.find()) {
			p(m.group());
			p(m.group(1));
		}
		*/

		//qulifiers
		/**
		 * Greedy quantifiers 贪婪数量，默认的
		 * Reluctant quantifiers 勉强数量？
		 * Possessive quantifiers 占有欲强的+
		 * */
		/*Pattern p2 = Pattern.compile("(.{3,10}?)[0-9]");
		Pattern p1 = Pattern.compile("(.{3,10})[0-9]");
		Pattern p = Pattern.compile("(.{3,10}+)[0-9]");
		String s = "aaa5bbbb68";
		Matcher m = p.matcher(s);
		if(m.find()) {
			p(m.group());
			p(m.start() + "-" + m.end());
		}else {
				p("not match!");
		}
		*/

		//non-capturing groups不是捕获组
        /*
		Pattern p1 = Pattern.compile(".{3}(?=a)");//a不算
		Pattern p3 = Pattern.compile("(?=a).{3}");//a算
		Pattern p2 = Pattern.compile(".{3}(?!a)");
		Pattern p4 = Pattern.compile(".{3}(?<=a)");//a算
		Pattern p = Pattern.compile(".{3}(?<!a)");//a不算
		String s = "444a66ba";
		Matcher m = p.matcher(s);
		while(m.find()) {
			p(m.group());
		}
		*/

		//back refenrences向前引用 \\1与第一个组一致 \\2与第二个组一致
		Pattern p1 = Pattern.compile("(\\d\\d)\\1");
		Pattern p = Pattern.compile("(\\d(\\d))\\2");
		String s = "1223";
		Matcher m = p.matcher(s);
		//p(m.matches());
		if(m.find())
		p(m.group());
		/**/

		//flags的简写
		//Pattern p = Pattern.compile("java", Pattern.CASE_INSENSITIVE);
		//p("Java".matches("(?i)(java)"));
	}

	public static void p(Object o) {
		System.out.println(o);
	}

}
